package com.caration.shell.preference;

import com.caration.sdk.utilities.DataUtils;
import com.caration.shell.R;
import com.caration.shell.config.CommonConfig;
import com.caration.shell.cr3d.SharedData;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.Preference;

public final class SettingDirectPreference {
	private static int sLastDirect;
	private static int sDirect;

	static int getDirect() {
		return sDirect;
	}

	static int getLastDirect() {
		return sLastDirect;
	}

	static void init(Preference preference) {
		setSummary(preference);
	}

	static void setDirect(int direct) {
		sDirect = direct;
	}

	public static void show(Context context, final Preference preference) {
		android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(
				context);
		builder.setTitle(R.string.mn_preference_direction);
		sDirect = sLastDirect = CommonConfig.getPrefDirection();
		String array[] = new String[2];
		array[0] = DataUtils.getString(context,
				R.string.mn_direction_preference_left);
		array[1] = DataUtils.getString(context,
				R.string.mn_direction_preference_right);
		builder.setSingleChoiceItems(array, sLastDirect,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						setDirect(which);
					}
				});
		builder.setPositiveButton(R.string.public_action_ok,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						if (getLastDirect() != getDirect()) {
							CommonConfig.setPrefDirection(getDirect());
							setSummary(preference);
						}
					}
				});
		builder.setNegativeButton(R.string.public_action_cancel, null);
		builder.create().show();
	}

	public static void setSummary(Preference preference) {
		StringBuilder sb = new StringBuilder(String.valueOf(DataUtils
				.getString(SharedData.getContext(),
						R.string.mn_preference_direction_summary)))
				.append(" - ");
		switch (CommonConfig.getPrefDirection()) {
		case 0:
			preference.setSummary(sb.append(
					DataUtils.getString(SharedData.getContext(),
							R.string.mn_direction_preference_left)).toString());
			break;

		case 1:
			preference
					.setSummary(sb.append(
							DataUtils.getString(SharedData.getContext(),
									R.string.mn_direction_preference_right))
							.toString());
			break;
		}
	}
}
